<?php
global $wpdb;

$table = $wpdb->prefix.'newsletter';

// khoi tao database
//add_action('init','newsletter_init_database' );
function newsletter_init_database()
{
	
	global $wpdb;
	// Đăng ký theo doi khoa hoc
	$newsletter = $wpdb->prefix.'newsletter';
	
	if ($wpdb->get_var("SHOW TABLES LIKE '$newsletter'") != $newsletter)
	{
		$wpdb->query("CREATE TABLE {$newsletter}
		(
		ID  mediumint(10) AUTO_INCREMENT PRIMARY KEY ,
		email 			VARCHAR(250)  NULL,
		status			VARCHAR(255) NULL,
		options			VARCHAR(255) NULL,		
		date			DATETIME
			
		)ENGINE=MyISAM DEFAULT CHARSET=utf8");
	}
}

////<!--  newsletter js  -->
////<script type="text/javascript" src="<?php echo get_bloginfo('template_url');/incs/newsletter/newsletter.js"></script>
// add new newsletter
if(wp_verify_nonce($_POST['act-newsletter'],'act-newsletter'))
{
	// add new
	$newsletter = trim($_POST['newsletter']);
	
	if(!is_email($newsletter))
	{
		$newsletterError = 'Email không đúng định dạng';
	}
	else 	
		$newsletterError = db_add_newsletter($table, $newsletter);
}




if($newsletterError!='')
{
	$html = '<p class="show-error-message">'.$newsletterError.'</p>';
	$html .= '
				<script type="text/javascript">
					alert("'.$newsletterError.'");
					window.scrollTo(0, document.body.scrollHeight);
				</script>              
			';
	
	
	echo $html;
	unset($html);
}
?>
